package com.yoosora.scheduled;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.yoosora.model.OiLPrice;
import com.yoosora.utils.ApiConst;
import com.yoosora.utils.RequestUtils;
import com.yoosora.utils.Strings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;


@Slf4j
@Configuration
@EnableScheduling
@EnableAsync
public class SaticScheduleTask {
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	//后续加入常量类	
	private final static String JUHE_OIL_PRE="JUHE_OIL_";
	//后续加入常量类
	private final static String JUHE_OIL_KEY = "07e4b8090124dbf87ee05a9a4c89df4f";
	
	private final static String HOLIDAY_PLAN="HOLIDAY_PLAN";
	
	@Scheduled(cron = "0 0 0/2 * * ?")
	@Async
	@PostConstruct
	public void getOilPrice(){
		Map<String,String> param = Maps.newHashMap();
		param.put("key",JUHE_OIL_KEY);
		String result = RequestUtils.builder().get().build(ApiConst.JUHE_OIL, Boolean.FALSE, null, param);
//		String result = "{\"reason\":\"success!\",\"result\":[{\"city\":\"北京\",\"92h\":\"7.03\",\"95h\":\"7.48\",\"98h\":\"8.46\",\"0h\":\"6.71\"},{\"city\":\"山西\",\"92h\":\"6.99\",\"95h\":\"7.54\",\"98h\":\"8.24\",\"0h\":\"6.73\"},{\"city\":\"天津\",\"92h\":\"7.02\",\"95h\":\"7.42\",\"98h\":\"8.64\",\"0h\":\"6.67\"},{\"city\":\"河北\",\"92h\":\"7.02\",\"95h\":\"7.42\",\"98h\":\"8.24\",\"0h\":\"6.67\"},{\"city\":\"河南\",\"92h\":\"7.03\",\"95h\":\"7.51\",\"98h\":\"8.17\",\"0h\":\"6.66\"},{\"city\":\"山东\",\"92h\":\"7.01\",\"95h\":\"7.52\",\"98h\":\"8.24\",\"0h\":\"6.66\"},{\"city\":\"浙江\",\"92h\":\"7.00\",\"95h\":\"7.45\",\"98h\":\"8.16\",\"0h\":\"6.66\"},{\"city\":\"上海\",\"92h\":\"6.99\",\"95h\":\"7.44\",\"98h\":\"8.39\",\"0h\":\"6.65\"},{\"city\":\"江苏\",\"92h\":\"7.00\",\"95h\":\"7.45\",\"98h\":\"8.43\",\"0h\":\"6.63\"},{\"city\":\"福建\",\"92h\":\"7.00\",\"95h\":\"7.47\",\"98h\":\"8.18\",\"0h\":\"6.67\"},{\"city\":\"安徽\",\"92h\":\"6.99\",\"95h\":\"7.50\",\"98h\":\"8.33\",\"0h\":\"6.70\"},{\"city\":\"湖北\",\"92h\":\"7.04\",\"95h\":\"7.53\",\"98h\":\"8.51\",\"0h\":\"6.66\"},{\"city\":\"江西\",\"92h\":\"6.99\",\"95h\":\"7.51\",\"98h\":\"9.01\",\"0h\":\"6.71\"},{\"city\":\"湖南\",\"92h\":\"6.99\",\"95h\":\"7.43\",\"98h\":\"8.23\",\"0h\":\"6.73\"},{\"city\":\"广西\",\"92h\":\"7.09\",\"95h\":\"7.66\",\"98h\":\"8.47\",\"0h\":\"6.73\"},{\"city\":\"广东\",\"92h\":\"7.05\",\"95h\":\"7.64\",\"98h\":\"8.78\",\"0h\":\"6.68\"},{\"city\":\"云南\",\"92h\":\"7.18\",\"95h\":\"7.70\",\"98h\":\"8.38\",\"0h\":\"6.75\"},{\"city\":\"贵州\",\"92h\":\"7.16\",\"95h\":\"7.56\",\"98h\":\"8.46\",\"0h\":\"6.78\"},{\"city\":\"重庆\",\"92h\":\"7.10\",\"95h\":\"7.50\",\"98h\":\"8.45\",\"0h\":\"6.74\"},{\"city\":\"四川\",\"92h\":\"7.13\",\"95h\":\"7.62\",\"98h\":\"8.28\",\"0h\":\"6.73\"},{\"city\":\"海南\",\"92h\":\"8.14\",\"95h\":\"8.65\",\"98h\":\"9.78\",\"0h\":\"6.76\"}],\"error_code\":0}";
		if(Strings.isNotEmpty(result)){
			OiLPrice oiLPrice = JSON.parseObject(result, OiLPrice.class);
			if(10012==oiLPrice.getError_code()){
				log.info(oiLPrice.getReason());
				return;
			}
			List<Map<String, String>> list = oiLPrice.getResult();
			list.forEach(o -> {
//				log.info("key:"+JUHE_OIL_PRE+o.get("city")+"value:"+o);
				redisTemplate.opsForValue().set(JUHE_OIL_PRE+ o.get("city"), JSON.toJSONString(o));
			});
			log.info("油价刷新成功!");
		}else{
			log.info("油价刷新接口异常");
		}
	}
	
	@Scheduled(cron = "1 0 0 * * ? ")
	@Async
	@PostConstruct
	public void getHoliday(){
		Map<String,String> param = Maps.newHashMap();
		param.put("t", String.valueOf(System.currentTimeMillis()));
		String hlHolidayStr = RequestUtils.builder().get().build(ApiConst.HOLIDAY_PLAN, Boolean.FALSE, null, param);
		if(Strings.isNotEmpty(hlHolidayStr)){
			JSONObject hlHoliday = new JSONObject(hlHolidayStr);
			String tts = (String)hlHoliday.get("tts");
			redisTemplate.opsForValue().set(HOLIDAY_PLAN, tts);
			log.info("多久放假获取成功!");
		}else{
			log.info("多久放假接口获取失败!");
		}
	}
	
}
